package com.advantage.accounts.transfer;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.DatePickerDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.res.Resources;
import android.os.Bundle;
import android.text.Editable;
import android.view.Gravity;
import android.view.View;
import android.widget.*;
import com.advantage.R;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;

/**
 * Created with IntelliJ IDEA.
 * User: hirsche
 * Date: 13/04/14
 * Time: 20:35
 * To change this template use File | Settings | File Templates.
 */
public class MoneyTransferActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.money_transfer_amount);
        TextView fromValue = (TextView) findViewById(R.id.from_account_value);
        fromValue.setText((String) getIntent().getExtras().get(MoneyTransferFragment.FROM_ACCOUNT));
        TextView toValue = (TextView) findViewById(R.id.to_account_value);
        toValue.setText((String) getIntent().getExtras().get(MoneyTransferFragment.TO_ACCOUNT));
        final TextView fromAvailable = (TextView) findViewById(R.id.from_available_value);
        fromAvailable.setText("$1,000,000");
        final TextView toAvailable = (TextView) findViewById(R.id.to_available_value);
        toAvailable.setText("$1,000,000");

        final Calendar myCalendar = Calendar.getInstance();
        final DatePickerDialog.OnDateSetListener date = new DatePickerDialog.OnDateSetListener() {

            @Override
            public void onDateSet(DatePicker view, int year, int monthOfYear,
                                  int dayOfMonth) {
                myCalendar.set(Calendar.YEAR, year);
                myCalendar.set(Calendar.MONTH, monthOfYear);
                myCalendar.set(Calendar.DAY_OF_MONTH, dayOfMonth);
                updateLabel(myCalendar);
            }

        };
        //When the editText is clicked then popup the DatePicker dialog to enable user choose the date
        final EditText dateOfTransValue = (EditText) findViewById(R.id.date_value);
        dateOfTransValue.setOnFocusChangeListener(new View.OnFocusChangeListener() {

            @Override
            public void onFocusChange(View v, boolean hasFocus) {
                // TODO Auto-generated method stub
                if (hasFocus) {
                    new DatePickerDialog(v.getContext(), date, myCalendar
                            .get(Calendar.YEAR), myCalendar.get(Calendar.MONTH),
                            myCalendar.get(Calendar.DAY_OF_MONTH)
                    ).show();
                }
            }
        });

        dateOfTransValue.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                new DatePickerDialog(v.getContext(), date, myCalendar
                        .get(Calendar.YEAR), myCalendar.get(Calendar.MONTH),
                        myCalendar.get(Calendar.DAY_OF_MONTH)
                ).show();
            }
        });


        final Button nextButtonAmount = (Button) findViewById(R.id.next_button_amount);
        nextButtonAmount.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(final View v) {
               final EditText amountValueText =  (EditText)findViewById(R.id.amount_value);
                final EditText dateValueText =  (EditText)findViewById(R.id.date_value);
                if (amountValueText == null || amountValueText.getText().toString().equals("")) {
                    Toast.makeText(v.getContext(),"Please fill in amount of money value",Toast.LENGTH_SHORT).show();
                } else if (dateValueText == null || dateValueText.getText().toString().equals("")) {
                    Toast.makeText(v.getContext(),"Please fill in date of transaction value",Toast.LENGTH_SHORT).show();
                } else {
                    AlertDialog.Builder adb = new AlertDialog.Builder(v.getContext());
                    adb.setTitle("Please confirm transaction");
                    adb.setMessage("Please, complete the information for your transfer. We are ready to set up this transfer for $"+amountValueText.getText().toString()+"\n" +
                            "Do you want to continue?");
                    adb.setPositiveButton("OK", new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int which) {
                            Intent intent = new Intent(v.getContext(),MoneyTransferCompletedActivity.class);
                            intent.putExtra(MoneyTransferFragment.FROM_ACCOUNT,(String) getIntent().getExtras().get(MoneyTransferFragment.FROM_ACCOUNT));
                            intent.putExtra(MoneyTransferFragment.TO_ACCOUNT,(String) getIntent().getExtras().get(MoneyTransferFragment.TO_ACCOUNT));
                            intent.putExtra(MoneyTransferFragment.AMOUNT,amountValueText.getText().toString());
                            intent.putExtra(MoneyTransferFragment.DATE_OF_TRANSACTION,dateValueText.getText().toString());
                            startActivity(intent);
                        } });
                    adb.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int which) {
                            dialog.cancel();
                        } });
                    AlertDialog alertDialog = adb.create();
                    alertDialog.show();
                }
            }
        });

    }

    private void updateLabel(Calendar myCalendar) {
        EditText dateOfTransValue = (EditText) findViewById(R.id.date_value);
        String myFormat = "MM/dd/yyyy"; //In which you need put here
        SimpleDateFormat sdf = new SimpleDateFormat(myFormat, Locale.US);
        dateOfTransValue.setText(sdf.format(myCalendar.getTime()));
    }
}


